package com.caocaog.market.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.caocaog.market.entity.GoodsChannelShip;
import com.caocaog.market.mapper.GoodsChannelShipMapper;
import com.caocaog.market.service.IGoodsChannelShipService;
import com.caocaog.market.vo.GoodBindChannelsVO;
import com.getui.push.v2.sdk.common.Strings;
import org.jeecg.common.constant.CacheConstant;
import org.jeecg.common.util.RedisUtil;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Nullable;
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description
 * @Author Van.Planifolia
 * @Date 2024/1/11
 * @Version 1.0
 */
@Service
public class GoodsChannelShipServiceImpl extends ServiceImpl<GoodsChannelShipMapper, GoodsChannelShip> implements IGoodsChannelShipService {

    @Lazy
    @Resource
    private IGoodsChannelShipService goodsChannelShipService;
    @Resource
    private RedisUtil redisUtil;

    /**
     * 商品绑定频道信息
     *
     * @param goodBindChannelsVO 商品绑定频道
     */
    @Override
    @Transactional
    public void goodBindChannels(GoodBindChannelsVO goodBindChannelsVO) {
        // 清理该商品之前绑定的信息
        String sourceChannelId = goodBindChannelsVO.getSourceChannelId();
        String goodId = goodBindChannelsVO.getGoodId();
        // 清理DB
        goodsChannelShipService.remove(new LambdaQueryWrapper<GoodsChannelShip>().eq(GoodsChannelShip::getGoodsId, goodId));
        // 清理Cache
        if(Strings.isNotBlank(sourceChannelId)){
            String sKey = CacheConstant.build(CacheConstant.GOODS_CHANNEL_MAP, sourceChannelId);
            redisUtil.del(sKey);
        }
        // 处理绑定的频道信息
        String channelIds = goodBindChannelsVO.getChannelIds();
        if (Strings.isBlank(channelIds))
            return;
        // 构造保存的DB实体与cache实体
        List<String> newChannelIdList = Arrays.stream(channelIds.split(",")).collect(Collectors.toList());
        List<GoodsChannelShip> goodsChannelShipList = newChannelIdList.stream().map(channelId -> new GoodsChannelShip().setGoodsId(goodId).setChannelId(channelId)).collect(Collectors.toList());
        // 清理新数据的缓存
        String[] newChannelKeyArray =  newChannelIdList.stream().map(e -> CacheConstant.build(CacheConstant.GOODS_CHANNEL_MAP, e)).toArray(String[]::new);
        redisUtil.del(newChannelKeyArray);
        goodsChannelShipService.saveBatch(goodsChannelShipList);
    }

    /**
     * 清除该频道下的所有商品绑定
     *
     * @param channelId 频道Id
     */
    @Override
    @Transactional
    public void clearThisChannelGoodsBind(String channelId) {
        clearGoodsCacheByChannelId(channelId);
        goodsChannelShipService.remove(new LambdaQueryWrapper<GoodsChannelShip>().eq(GoodsChannelShip::getChannelId, channelId));
    }

    /**
     * 清理商品缓存根据频道ID,若不传channelId则清理全部的缓存
     *
     * @param channelId 频道ID
     */
    @Override
    public void clearGoodsCacheByChannelId(@Nullable String channelId) {
        if (Strings.isBlank(channelId)) {
            redisUtil.del(CacheConstant.GOODS_CHANNEL_MAP);
            return;
        }
        redisUtil.hdel(CacheConstant.GOODS_CHANNEL_MAP, channelId);
    }

    /**
     * 添加商品绑定信息
     *
     * @param goodsChannelShip 商品绑定信息
     */
    @Override
    public void saveGoodChannelShip(GoodsChannelShip goodsChannelShip) {
        String channelId = goodsChannelShip.getChannelId();
        redisUtil.del(CacheConstant.build(CacheConstant.GOODS_CHANNEL_MAP, channelId));
        save(goodsChannelShip);
    }
}
